<!--API资产划转-->
<template>
  <div class="bg bg1">
    <template v-if="api">
      
      <!--币-->
      <div class="coin bg2">
        <text class="font28 text2 width110">{{T('币种')}}</text>
        <text class="font30 text1">{{coin}}</text>
      </div>

      <!--账户-->
      <div class="account bg2">

        <div class="acenter">
          <text class="font28 text2">{{T('从')}}</text>
          <div class="height8"></div>
          <LImage class="dian-img" :src="Config.img('zc_huazhuan1.png')"></LImage>
          <div class="height8"></div>
          <text class="font28 text2">{{T('到')}}</text>
        </div>

        <div>
          <div class="tap">
            <text class="font30 text1">{{T(accountA)}}</text>
          </div>

          <div class="line line1"></div>

          <div class="tap">
            <text class="font30 text1">{{T(accountB)}}</text>
          </div>
        </div>

        <LImage class="rotate-img" ref="rotate" :src="Config.img('zc_huazhuan.png')" @click="rotateClick"></LImage>
      </div>
      
      <!--数量-->
      <div class="input bg2">
        <text class="font28 text2 width110">{{T('数量')}}</text>
        <input class="input-content font28 text1" ref="input" v-model="num" type="number" @input="onInput" @focus="onFocus" @blur="onBlur">
        <text class="font28 text3">{{'  ' + coin}}</text>
        <div class="input-line line1"></div>
        <div class="input-all" @click="allClick">
          <text class="font24 main-t1">{{T('全部')}}</text>
        </div>
      </div>

      <div class="height20"></div>
      <div class="row">
        <div class="width40"></div>
        <text class="font24 text3">{{T('可用') + ' ' + available + ' ' + coin}}</text>
      </div>
      
      <!--按钮-->
      <div class="but main-bt1" @click="okClick">
        <text class="font30 text1">{{T('确认')}}</text>
      </div>
    </template>
  </div>
</template>

<style src="@/_css/style.css" ></style>
<style scoped>
.bg {
  flex: 1;
  padding-left: 20px;
  padding-right: 20px;
}

/*********************************** 币*/
.coin {
  flex-direction: row;
  align-items: center;

  height: 98px;
  border-radius: 24px;

  padding-left: 40px;
  padding-right: 60px;

  margin-top: 30px;
}
.arrow-right-img {
  width: 13px;
  height: 20px;
}

/*********************************** 账户*/
.account {
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  border-radius: 24px;

  padding-left: 40px;
  padding-right: 40px;

  margin-top: 40px;
}
.tap {
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  width: 400px;
  height: 110px;
}
.line {
  width: 400px;
  height: 1px;
}
.arrow-down-img {
  width: 17px;
  height: 21px;
}
.rotate-img {
  width: 60px;
  height: 60px;
}
.dian-img {
  width: 19px;
  height: 23px;

  margin-top: 14px;
  margin-bottom: 14px;
}

/*********************************** 币*/
.input{
  flex-direction: row;
  align-items: center;
  height: 98px;
  border-radius: 24px;
  padding-left: 40px;

  margin-top: 40px;
}
.input-content {
  height: 88px;
  flex: 1;
  text-align: right;
}
.input-line{
  width: 2px;
  height: 24px;

  margin-left: 20px;
}
.input-all {
  flex-direction: row;
  justify-content: center;
  align-items: center;
  height: 88px;
  padding-left: 20px;
  padding-right: 20px;
  margin-right: 20px;
}

/*********************************** 按钮*/
.but{
  justify-content: center;
  align-items: center;
  height: 80px;
  border-radius: 12px;

  margin-top: 80px;
}

/*********************************** 间距*/
.width110{
  width: 110px;
}
.height8 {
  height: 8px;
}
</style>

<script>

import jnavigator from "@/_js/navigator";
import jhttps from "@/_js/https";
import jconfig from "@/_js/config";
import jmath from "@/_js/math";
import jmodal from "@/_js/modal";
import japi from "@/_js/api";
import janimation from "@/_js/animation"; 
import jdebug from "@/_js/debug"; 

const weexFunction = weex.requireModule("WeexFunction");
const globalEvent = weex.requireModule('globalEvent');

import I18N from "@/_i18n/i18n";
var T = I18N.t;
weexFunction.setTitle(T('资产划转'));

export default {
  
  data() {
    return {
      T:T,
      Config:jconfig,

      accounts:['资金账户', '统一账户'],
      accountA:'资金账户',
      accountB:'统一账户',
      accountType:{'资金账户':'spot', '统一账户':'linear-swap'},

      apiId:undefined,
      api:undefined,
      
      coin:'USDT', //币
      available:'0', //可划转数量
      num:'', //划转数量
    };
  },

  created() {
    
    var param = weex.config;
    this.apiId = param.apiId;

    jconfig.init(()=>{
      this.initData();
    }, [jconfig.OPT_CURRENT_BZ]);

    //Api更新通知
    globalEvent.addEventListener("apiUp",(e)=>{
      this.initData();
    });
  },

  methods: {

    initData() {
      japi.getApi(this.apiId, 'all', (api)=>{
        this.initAvailable(api);
        this.api = api;
      });
    },

    //初始化可用余额
    initAvailable(api) {
      
      var coins = this.accountA == '资金账户' ? api.moneyAssets : api.accountAssets;
      for (let index = 0; index < coins.length; index++) {
        var coinBean = coins[index];

        if (coinBean.currency == this.coin) {

          var coinBean = coins[index];
          var withdrawAvailable = coinBean.availableReal;
          withdrawAvailable = Number(withdrawAvailable) < 0 ? '0' : withdrawAvailable;
          this.available = jmath.gfCoin(withdrawAvailable, coinBean.currency, 2);
          return;
        }
      }

      this.available = '0.00'
    },

    /********************************** HTTP*/
    //http获取服务器公钥
    httpGetPublicKey(){
      
      jmodal.showLoad();
      var param = {'serverType':'accountServer'};
      jhttps.post("assetsV2/getPublicKey",param,true,(data)=>{

        if(data.status == 200){
          data = data.data;
          var key =  data['publicKey'];
          japi.rsaApi(this.api, key, (rsa)=>{
            this.httpAccountTransfer(rsa);
          });

        } else {
          jmodal.hideLoad();
          jmodal.toast(data.msg);
        }
      });
    },

    //http资产划转
    httpAccountTransfer(apiData){
      var paramStr = {'from':this.accountType[this.accountA],
                      'to':this.accountType[this.accountB],
                      'currency':this.coin,
                      'amount':this.num};
      paramStr = JSON.stringify(paramStr);
      var param = {'apiData':apiData, 'param':paramStr};
      jhttps.postX("assetsV2/accountTransfer",param,true,(data)=>{

        if(data.status == 200){
          
          //同步完成
          var ok = ()=>{
            setTimeout(() => {
              this.num = '';
              jmodal.hideLoad();
              jmodal.toast(T('划转成功'));
              jnavigator.back();
            }, 3000);
          };
          japi.synchroApi(this.apiId, ok, ok);
        }  else if (data.status == 6074) {

          jmodal.hideLoad();
          jmodal.alert({'message':'API资产已改变，同步资产后重新尝试划转', 'btn':'同步资产'}, ()=>{

            jmodal.showLoad();
            japi.synchroApi(this.apiId, ()=>{
              setTimeout(() => {
                jmodal.hideLoad();
              }, 3000);
            }, ()=>{
              jmodal.hideLoad();
              jmodal.toast(T('同步失败'));
            });
          })
        } else {
          jmodal.hideLoad();
          jmodal.toast(data.msg);
        }
      });
    },

    /********************************** 输入框监听*/
    //键盘隐藏
    onBlur(){
      this.num = jmath.toString(this.num);
    },

    /********************************** 点击触发*/
    //账户A
    account1(){
      if (this.accountA == '资金账户'){
        return;
      }

      var accounts = [];
      for (let index = 0; index < this.accounts.length; index++) {
        var str = this.accounts[index];
        accounts.push(T(str));
      }

      jmodal.actionSheet(accounts, T(this.accountA), (value)=>{
        
        for (let index = 0; index < accounts.length; index++) {
          var str = accounts[index];
          if (str == value) {
            value = this.accounts[index];
            break;
          }
        }

        if (this.accountA == value) {
          return;
        }

        if (this.accountB == value) {
          this.accountB = this.accountA;
          this.accountA = value;
        } else {
          this.accountA = value;
        }

        if (value != '资金账户' && this.accountB != '资金账户') {
          this.accountB = '资金账户';
        }

        this.num = '';
        this.initAvailable(this.api);
      });
    },

    //账户2
    account2(){
      if (this.accountB == '资金账户'){
        return;
      }


      var accounts = [];
      for (let index = 0; index < this.accounts.length; index++) {
        var str = this.accounts[index];
        accounts.push(T(str));
      }

      jmodal.actionSheet(accounts, T(this.accountB), (value)=>{
        
        for (let index = 0; index < accounts.length; index++) {
          var str = accounts[index];
          if (str == value) {
            value = this.accounts[index];
            break;
          }
        }
        
        if (this.accountB == value) {
          return;
        }
        
        if (this.accountA == value) {
          this.accountA = this.accountB;
          this.accountB = value;
        } else {
          this.accountB = value;
        }
        
        if (value != '资金账户' && this.accountA != '资金账户') {
          this.accountA = '资金账户';
        }

        this.num = '';
        this.initAvailable(this.api);
      });
    },

    //旋转
    rotateClick() {

      janimation.start('api_transfer', this.$refs.rotate, 250, 180, 1);
      
      var accountB = this.accountA
      this.accountA = this.accountB;
      this.accountB = accountB;

      this.num = '';
        this.initAvailable(this.api);
    },

    //全部
    allClick() {
      this.num = this.available;
    },

    //确认
    okClick() {
      this.$refs.input.blur();
      
      
      if (this.num.length <= 0) {
        jmodal.toast(T('请输入划转数量'));
      } else if (Number(this.num) == 0) {
        jmodal.toast(T('划转数量不合法'));
      } else if (Number(this.num) > Number(this.available)) {
        jmodal.toast(T('可用余额不足'));
      } else {
        this.httpGetPublicKey();
      }
    }
  },
};

</script>
